<?php


namespace app\api\controller\v1;


use app\api\model\RoleModel;
use app\api\model\StaffModel;
use app\common\controller\UserBaseController;
use ApiOper;

class RoleController extends UserBaseController
{

    function listAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'sk' => 'optstring',
            'pno' => 'int',
            'psize' => 'int',
            'type' => 'optstring',
            'is_base_role' => 'optint',
        );

        $this->check_params();
        $this->check_token();

        $role_id = ApiOper::instance()->userinfo()['cc_role_id'];
        $this->argv['level'] = (new RoleModel())->role_level($role_id);

        $list = (new RoleModel())->query_list($this->city_id, $this->argv, $this->argv['pos'], $this->argv['limit']);
        $this->resp['list'] = $list;
        $this->resp['total'] = count($list);

        return $this->ret_resp();
    }

    function addAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'state' => 'int',
            'uri_ids' => 'intarray',
            'remark' => 'string',
            'type' => 'string',
            'is_base_role' => ['optint', 0],
        );

        $this->check_params();
        $this->check_token();

        $city_id = $this->city_id;
        $level = 100;
        if ($this->argv['is_base_role']) {
            $city_id = null;
            $level = 2;
        }

        $data = [
            'city_id' => $city_id,
            'name' => $this->argv['name'],
            'state' => $this->argv['state'],
            'uri_ids' => $this->argv['uri_ids'],
            'remark' => $this->argv['remark'],
            'type' => $this->argv['type'],
            'level' => $level,
        ];

        if ($this->argv['type'] == 'CS'){
            $data['city_id'] = null;
        }

        $info = (new RoleModel())->add($data);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function updateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
            'name' => 'optstring',
            'remark' => 'optstring',
            'state' => 'optint',
            'uri_ids' => 'optintarray',
        );

        $this->check_params();
        $this->check_token();

        $role_info = (new RoleModel())->query_info($this->city_id, $this->argv['id']);
        if (empty($role_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询角色失败!");
        }
        if ($role_info['city_id'] === null) {
            //throw_myexception(X_ERROR, "基础角色，不能修改!");
        }

        $info = (new RoleModel())->update($role_info['city_id'], $this->argv['id'], $this->argv);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function infoAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();


        $info = (new RoleModel())->query_info($this->city_id, $this->argv['id']);
        if (empty($info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询角色信息失败！");
        }

        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function add_role_adminAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'city_id' => 'string',
        );

        $this->check_params();
        $this->check_token();


        $info = (new RoleModel())->create_admin_role($this->city_id);
        $this->resp['info'] = $info;

        return $this->ret_resp();
    }

    function delAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'string',
        );

        $this->check_params();
        $this->check_token();

        $role_info = (new RoleModel())->query_info($this->city_id, $this->argv['id']);
        if (empty($role_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询角色失败!");
        }

        if ($role_info['city_id'] == null) {
            //throw_myexception(ERROR_QUERY_INFO, "没有权限！");
        }

        $condition = [];
        if ($role_info['type'] == 'OPS') {
            $condition = [
                'ops_role_id' => $this->argv['id']
            ];
        } else if ($role_info['type'] == 'CC') {
            $condition = [
                'cc_role_id' => $this->argv['id']
            ];
        }


        $staffes = (new StaffModel())->query_list($this->city_id, $condition, 0, 1);
        if (!empty($staffes)) {
            $this->throw_myexception(X_ERROR, "该角色存在用户,不能删除!");
        }

        $data = [
            'is_deleted' => 1,
        ];

        (new RoleModel())->update($role_info['city_id'], $this->argv['id'], $data);

        return $this->ret_resp();
    }

}